package hongcaosp.app.android.video.record;

import android.Manifest;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.tencent.liteav.demo.common.utils.TCConstants;
import com.tencent.liteav.demo.ugccommon.TCVideoEditUtil;
import com.tencent.liteav.demo.ugccommon.TCVideoEditerListAdapter;
import com.tencent.liteav.demo.ugccommon.TCVideoFileInfo;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import hongcaosp.app.android.R;
import xlj.lib.android.base.component.BaseActivity;
import xlj.lib.android.base.toastcompat.ToastManager;
import xlj.lib.android.base.utils.StatusBarUtil;

public class SelectVideoActivity extends BaseActivity implements SelectVideoPresenter.SearchVideoCallBack {
    private RecyclerView recyclerView;
    private TCVideoEditerListAdapter mAdapter;
    private SelectVideoPresenter selectVideoPresenter;
    @Override
    protected void initData(Intent intent) {

    }

    @Override
    protected void initView() {
        StatusBarUtil.setStatusBarDarkTheme(this,false);

        recyclerView = findViewById(com.tencent.liteav.demo.common.R.id.recycler_view);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        mAdapter = new TCVideoEditerListAdapter(this);
        mAdapter.setMultiplePick(false);
        recyclerView.setAdapter(mAdapter);

        selectVideoPresenter = new SelectVideoPresenter(this);

        AndPermission.with(this).runtime().permission(Manifest.permission.READ_EXTERNAL_STORAGE).onGranted(new Action<List<String>>() {
            @Override
            public void onAction(List<String> data) {
                selectVideoPresenter.fresh(SelectVideoActivity.this);
            }
        }).onDenied(new Action<List<String>>() {
            @Override
            public void onAction(List<String> data) {
                ToastManager.getInstance().showToast("获取视频列表失败");
            }
        }).start();

        findViewById(R.id.back_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        findViewById(R.id.select).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TCVideoFileInfo fileInfo = mAdapter.getSingleSelected();
                if (fileInfo == null) {
                    ToastManager.getInstance().showToast("请选择一个视频");
                    return;
                }
                if (TCVideoEditUtil.isVideoDamaged(fileInfo)) {
                    ToastManager.getInstance().showToast("该视频文件已经损坏");
                    return;
                }
                File file = new File(fileInfo.getFilePath());
                if (!file.exists()) {
                    ToastManager.getInstance().showToast("选择的文件不存在");
                    return;
                }

                Intent intent = new Intent(getApplicationContext(), TCVideoPreviewActivity.class);
//            Intent intent = new Intent();
//            intent.setAction("com.tencent.liteav.demo.videopreview");
                intent.putExtra(TCConstants.VIDEO_RECORD_TYPE, TCConstants.VIDEO_RECORD_TYPE_EDIT);
//                intent.putExtra(TCConstants.VIDEO_RECORD_RESULT, mTXRecordResult.retCode);
//                intent.putExtra(TCConstants.VIDEO_RECORD_DESCMSG, mTXRecordResult.descMsg);
                intent.putExtra(TCConstants.VIDEO_RECORD_VIDEPATH, fileInfo.getFilePath());
                intent.putExtra(TCConstants.VIDEO_RECORD_COVERPATH, fileInfo.getThumbPath());
                intent.putExtra(TCConstants.VIDEO_RECORD_DURATION, fileInfo.getDuration());
//                if (mRecommendQuality == TXRecordCommon.VIDEO_QUALITY_LOW) {
//                    intent.putExtra(TCConstants.VIDEO_RECORD_RESOLUTION, TXRecordCommon.VIDEO_RESOLUTION_360_640);
//                } else if (mRecommendQuality == TXRecordCommon.VIDEO_QUALITY_MEDIUM) {
//                    intent.putExtra(TCConstants.VIDEO_RECORD_RESOLUTION, TXRecordCommon.VIDEO_RESOLUTION_540_960);
//                } else if (mRecommendQuality == TXRecordCommon.VIDEO_QUALITY_HIGH) {
//                    intent.putExtra(TCConstants.VIDEO_RECORD_RESOLUTION, TXRecordCommon.VIDEO_RESOLUTION_720_1280);
//                } else {
//                    intent.putExtra(TCConstants.VIDEO_RECORD_RESOLUTION, TXRecordCommon.VIDEO_RESOLUTION_720_1280);
//                }
                startActivity(intent);
            }
        });
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_select_video;
    }

    @Override
    public void onGetVideos(ArrayList<TCVideoFileInfo> result) {
        mAdapter.addAll(result);
    }
}
